package com.example.testapplication;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class ViewpagerActivity extends AppCompatActivity {
    public static final int ITEM = 0;
    private ViewPager mViewPager;
    // 设置一个数组
    private int[] mLayoutIDs = {
            R.layout.view_first,
            R.layout.view_second,
            R.layout.view_thrid,
    };
    private List<View> mViews;
    private ViewGroup mDotViewGroup;
    private List<ImageView> mDotViews = new ArrayList<>();

    public ViewpagerActivity() {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view_adapter);
        // 获取到viewPager
         mViewPager = (ViewPager) findViewById(R.id.view_pager);
         // 初始化数据
        mViews= new ArrayList<>();

        mDotViewGroup = (ViewGroup) findViewById(R.id.dot_layout);
        for (int index = 0;index < mLayoutIDs.length; index++){
//            final View view = getLayoutInflater().inflate(mLayoutIDs[index],null);
//            mViews.add(view);
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.mipmap.ic_launcher);
            mViews.add(imageView);
            ImageView dot = new ImageView(this);
            dot.setImageResource(R.mipmap.ic_launcher);
            dot.setMaxHeight(100);  // 限制最大高度
            dot.setMaxWidth(100);  // 限制最大宽度
            // 设置宽高
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80,80);
            layoutParams.leftMargin = 20;
            dot.setLayoutParams(layoutParams);
            dot.setEnabled(false);
            mDotViewGroup.addView(dot);
            mDotViews.add(dot);

        }
        // 设置 adapter
         mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setCurrentItem(ITEM); // 设置初始页
        setDotViews(ITEM); // 设置第一个被选中

        // 设置 滚动 选中的小点
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            // 滚动
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            // 选中
            @Override
            public void onPageSelected(int position) {

                setDotViews(position);


            }

            // 滚动中状态变化
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void setDotViews(int position){
        // 遍历
        for (int index = 0; index < mDotViews.size();index ++){
            mDotViews.get(index).setImageResource(position == index ?R.mipmap.diglett:R.mipmap.ic_launcher);
        }
    }


    PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return mLayoutIDs.length;  // 所有view数量
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            // 添加视图
            View child = mViews.get(position);
            container.addView(child);
            return child;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(mViews.get(position));
        }
    };
}
